package com.example.livingadminmaster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.livingadminmaster.common.IdWorker;
import com.example.livingadminmaster.mapper.MaintenanceMapper;
import com.example.livingadminmaster.pojo.Maintenance;
import com.example.livingadminmaster.service.MaintenanceServoice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class MaintenanceServoiceImpl extends ServiceImpl<MaintenanceMapper, Maintenance> implements MaintenanceServoice{
    IdWorker idWorker = new IdWorker(1,1);
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Override
    public List<Maintenance> getAllMaintenance() {
       List<Maintenance> maintenances= maintenanceMapper.selectList(null);
       return maintenances;
    }

    @Override
    public Maintenance getMaintenanceById(long maintenance_id) {
        return maintenanceMapper.selectById(maintenance_id);
    }

    @Override
    public Maintenance getMaintenanceByUsername(String username) {
        return maintenanceMapper.selectOne(new LambdaQueryWrapper<Maintenance>().eq(Maintenance::getUsername,username));
    }

    @Override
    public int addMaintenance(Maintenance maintenance) {
        long maintenance_id = idWorker.nextId();
       maintenance.setMaintenanceId(maintenance_id);
        return maintenanceMapper.insert(maintenance);
    }

    @Override
    public boolean deleteMaintenanceById(long maintenance_id) {
        return this.removeById(maintenance_id);
    }

    @Override
    @Transactional
    public int updateMaintenance(Maintenance maintenance) {
        return maintenanceMapper.updateById(maintenance);
    }

    @Override
    public boolean updateMaintenanceStatus(long maintenance_id, boolean status) {
        LambdaUpdateWrapper<Maintenance> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Maintenance::getMaintenanceId,maintenance_id).set(Maintenance::getStatus,status);
        return maintenanceMapper.update(null,updateWrapper)>0;
    }
}
